/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta.freezay.admin.unit;

import de.swta.*;
import de.swta.freezay.admin.category.CategoryEditPanel;
import de.swta.freezay.components.ActionPanel;
import de.swta.freezay.components.OptionContentPanel;
import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.dbEntities.Category;
import de.swta.freezay.database.dbEntities.Unit;
import de.swta.freezay.database.wicketAdapter.CategoryDataProvider;
import de.swta.freezay.database.wicketAdapter.UnitDataProvider;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.AbstractReadOnlyModel;

/**
 *
 * @author Jan Korreck
 */
public class UnitView extends Panel {

    private final ModalWindow modalWindow = new ModalWindow("modal");
    private WebMarkupContainer theContainer;
    private DataView<Unit> unitView;

    public UnitView() {
        this("UnitView");
    }

    public UnitView(String id) {

        super(id);
        init();
    }

    private void init() {

        add(modalWindow);

        UnitDataProvider dataProvider = new UnitDataProvider();

        unitView = new DataView<Unit>("unit",
                dataProvider, 15) {

            @Override
            protected void populateItem(final org.apache.wicket.markup.repeater.Item<Unit> item) {

                final Unit u = item.getModelObject();

                item.add(new Label("Name", u.getName()));


                item.add(new ActionPanel("Action", "Editieren", new AjaxLink("select") {

                    @Override
                    public void onClick(AjaxRequestTarget target) {

                        modalWindow.setContent(new UnitEditPanel(modalWindow.getContentId(), u.getId(), true) {

                            @Override
                            public void saveLinkClicked(AjaxRequestTarget target) {
                                modalWindow.close(target);
                                target.addComponent(UnitView.this.theContainer);
                            }
                        });

                        modalWindow.setTitle("Einheit editieren");
                        modalWindow.setInitialHeight(150);
                        modalWindow.setInitialWidth(250);
                        modalWindow.show(target);
                    }
                }));

                item.add(new ActionPanel("Action2", "Löschen", new AjaxLink("select") {

                    @Override
                    public void onClick(AjaxRequestTarget target) {


                        modalWindow.setContent(new OptionContentPanel(modalWindow.getContentId()) {

                            public void onSelect(AjaxRequestTarget target, String selection) {

                                if (!JPAController.UnitJpaController.isUsed(u.getId())) {
                                    try {
                                        JPAController.UnitJpaController.destroy(u.getId());
                                    } catch (NonexistentEntityException ex) {
                                        Logger.getLogger(UnitView.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                    modalWindow.close(target);
                                    target.addComponent(UnitView.this.theContainer);
                                    
                                } else {
                                    error("Löschen nicht möglich \n " +
                                            "Die Einheit wird bereits verwendet");
                                }
                            }

                            public void onCancel(AjaxRequestTarget target) {
                                modalWindow.close(target);
                            }
                        });

                        ((OptionContentPanel) modalWindow.get(
                                modalWindow.getContentId())).setMessageText(
                                "Wollen sie die Einheit '" + u.getName()
                                + "' löschen?");
                        
                        modalWindow.setTitle("Einheit löschen");

                        modalWindow.setInitialHeight(200);
                        modalWindow.setInitialWidth(300);
                        modalWindow.show(target);
                    }
                }));


                item.add(new AttributeModifier("class", true,
                        new AbstractReadOnlyModel<String>() {

                            @Override
                            public String getObject() {
                                return (item.getIndex() % 2 == 1) ? "even" : "odd";
                            }
                        }));

            }
        };

        theContainer = new WebMarkupContainer("theContainer");
        theContainer.setOutputMarkupId(true);
        theContainer.add(unitView.setOutputMarkupId(true));
        theContainer.add(createOrderByBorder("orderByUnitName", "name",
                dataProvider, unitView).setOutputMarkupId(true));
        
        add(new PagingNavigator("pagenavigation_top", unitView).setOutputMarkupId(true));
        add(new PagingNavigator("pagenavigation_bottom", unitView).setOutputMarkupId(true));
        add(theContainer);

        add(new AjaxLink("addUnit") {

            @Override
            public void onClick(AjaxRequestTarget target) {

                modalWindow.setContent(new UnitEditPanel(modalWindow.getContentId(), -1, false) {

                    @Override
                    public void saveLinkClicked(AjaxRequestTarget target) {
                        modalWindow.close(target);
                        target.addComponent(UnitView.this.theContainer);
                    }
                });

                modalWindow.setTitle("Kategorie erstellen");
                modalWindow.show(target);
            }
        });
    }

    private final OrderByBorder createOrderByBorder(final String id,
            final String table, final ISortStateLocator sortStateLocator,
            final DataView<?> view) {

        return new OrderByBorder(id, table, sortStateLocator) {

            @Override
            protected void onSortChanged() {
                view.setCurrentPage(0);
            }
        };
    }
}
